package com.manong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.manong.domain.Employee;
import com.manong.exception.BusinessExp;
import com.manong.mapper.EmployeeMapper;
import com.manong.query.QueryObject;
import com.manong.service.IEmployeeService;
import com.manong.vo.AdminVo;
import com.manong.vo.EmployeeRoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    @Transactional
    public void save(EmployeeRoleVo employeeRoleVo) {
        // 先拿得到employee对象
        Employee employee = employeeRoleVo.getEmployee();
        // 把employee保存到数据库中
        employeeMapper.insert(employee);
        Long[] roleIds = employeeRoleVo.getRoleIds();
        // 维护关系-> 就往中间表中插入数据
        if(roleIds != null) {
//            for(Long roleId : roleIds) {
//                // 往中间表插入数据   employee_role(employee_id, role_id)
//                employeeMapper.insertRelation(employee.getId(), roleId);
//            }
            // 批量插入
            employeeMapper.insertBatchRelation(employee.getId(), roleIds);
        }
    }

    @Override
    public void delete(Long id) {
        // 参数校验
        if(id == null) {
            throw new BusinessExp("非法操作");
        }
        int i = employeeMapper.deleteByPrimaryKey(id);
        if(i == 0){
            throw new BusinessExp("删除失败");
        }
        // 删除关系表数据
        employeeMapper.deleteRelation(id);
    }

    @Override
    public Employee get(Long id) {
        if(id == null) {
            throw new BusinessExp("参数不能为空");
        }
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        if(employee == null) {
            throw new BusinessExp("非法操作");
        }
        return employee;
    }

    @Override
    public void update(EmployeeRoleVo employeeRoleVo) {
        // vo对象中获取employee对象
        Employee employee = employeeRoleVo.getEmployee();
        employeeMapper.updateByPrimaryKey(employee);
        Long[] roleIds = employeeRoleVo.getRoleIds();
        // 先删除旧关系，在维护新关系
        employeeMapper.deleteRelation(employee.getId());

        // 维护关系-> 就往中间表中插入数据
        if(roleIds != null) {
//            for(Long roleId : roleIds) {
//                // 往中间表插入数据   employee_role(employee_id, role_id)
//                employeeMapper.insertRelation(employee.getId(), roleId);
//            }
            // 批量插入
            employeeMapper.insertBatchRelation(employee.getId(), roleIds);
        }
    }

    @Override
    public List<Employee> getAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo){
        if(qo.getPageNum() == -1) {
            return new PageInfo(employeeMapper.selectForList(qo));
        } else {
            PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
            return new PageInfo(employeeMapper.selectForList(qo));
        }
    }

    @Override
    public void saveOrUpdate(EmployeeRoleVo employeeRoleVo) {
        // 1 参数校验
        if(employeeRoleVo == null) {
            throw new BusinessExp("非法操作");
        }
        if(employeeRoleVo.getEmployee() == null) {
            throw new BusinessExp("非法操作");
        }
        if(employeeRoleVo.getEmployee().getId() == null) {
            // 调用新增方法
            this.save(employeeRoleVo);
        } else {
            //调用编辑方法
            this.update(employeeRoleVo);
        }
    }

    @Override
    public void updateState(AdminVo adminVo) {
        // 1 参数校验
        if(adminVo == null) {
            throw  new BusinessExp("参数不能为空");
        }
        // 更新数据库管理员状态，
        int count = employeeMapper.updateState(adminVo.getId(), adminVo.getIsAdmin());
        if(count == 0) {
            throw new BusinessExp("更新失败");
        }
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.login(username, password);
    }


}
